package com.example.onlinemarket.util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.example.onlinemarket.activity.BaseActivity;
import com.example.onlinemarket.activity.OrderActivity;
import com.example.onlinemarket.activity.ShopActivity;
import com.example.onlinemarket.activity.ShowImageActivity;
import com.example.onlinemarket.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.example.onlinemarket.Application.showProgressDialog;
import static com.example.onlinemarket.util.HttpUtil.getShop;

public class ActivityUtil {

    private static final List<BaseActivity> activityList;
    public static BaseActivity contextActivity;

    static {
        activityList = new ArrayList<>();
    }

    //从ContextActivity跳转到目标Activity
    public static void jumpToActivity(Class targetActivityClazz){
        new Thread(() -> {
            if(contextActivity!=null){
                if(!contextActivity.getClass().equals(targetActivityClazz)){
                    Intent jumpIntent = new Intent(contextActivity, targetActivityClazz);
                    contextActivity.startActivity(jumpIntent);
                }else{
                    contextActivity.runOnUiThread(() -> Toast.makeText(
                            contextActivity, "已经在当前Activity了", Toast.LENGTH_SHORT).show());
                }
            }
        }).start();
    }

    //从ContextActivity跳转到目标Activity
    public static void jumpToActivity(Class targetActivityClazz, Map<String, String> map){
        new Thread(() -> {
            if(contextActivity!=null){
                if(!contextActivity.getClass().equals(targetActivityClazz)){
                    Intent jumpIntent = new Intent(contextActivity, targetActivityClazz);
                    for(Map.Entry<String, String> entry: map.entrySet()){
                        jumpIntent.putExtra(entry.getKey(), entry.getValue());
                    }
                    contextActivity.startActivity(jumpIntent);
                }else{
                    contextActivity.runOnUiThread(() -> Toast.makeText(
                            contextActivity, "已经在当前Activity了", Toast.LENGTH_SHORT).show());
                }
            }
        }).start();
    }

    //展示大图
    public static void showBigImage(String imgPath){
        showProgressDialog(contextActivity);
        Intent imgIntent = new Intent(contextActivity, ShowImageActivity.class);
        imgIntent.putExtra("imgPath", imgPath);
        contextActivity.startActivity(imgIntent);
    }

    //跳转到商店页面
    public static void goShop(Activity activity, int shopId){
        activity.runOnUiThread(() -> showProgressDialog(activity));
        User shop = getShop(shopId);
        Intent shopIntent = new Intent(activity, ShopActivity.class);
        shopIntent.putExtra("shop", shop);
        activity.startActivity(shopIntent);
    }

    //跳转到商店页面
    public static void goShop(Activity activity, User shop){
        activity.runOnUiThread(() -> showProgressDialog(activity));
        Intent shopIntent = new Intent(activity, ShopActivity.class);
        shopIntent.putExtra("shop", shop);
        activity.startActivity(shopIntent);
    }

    //跳转到订单页面
    public static void goOrder(Activity activity, int orderStatus){
        activity.runOnUiThread(() -> showProgressDialog(activity));
        Intent orderIntent = new Intent(activity, OrderActivity.class);
        orderIntent.putExtra("status", orderStatus);
        activity.startActivity(orderIntent);
    }

    public static void jumpToActivityForResult(Class targetActivityClazz, int requestCode){
        new Thread(() -> {
            if(contextActivity!=null){
                Intent jumpIntent = new Intent(contextActivity, targetActivityClazz);
                contextActivity.startActivityForResult(jumpIntent, requestCode);
            }
        }).start();
    }

    public static void jumpToActivityForResult(Class targetActivityClazz, Map<String, String> map, int requestCode){
        new Thread(() -> {
            if(contextActivity!=null){
                Intent jumpIntent = new Intent(contextActivity, targetActivityClazz);
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    jumpIntent.putExtra(entry.getKey(), entry.getValue());
                }
                contextActivity.startActivityForResult(jumpIntent, requestCode);
            }
        }).start();
    }

    public static void addActivity(BaseActivity activity){
        activityList.add(activity);
    }

    public static void removeActivity(BaseActivity activity){
        if(!activity.isFinishing())
            activity.finish();
        activityList.remove(activity);
    }

    public static void removeAll(){
        for(BaseActivity activity: activityList){
            if(!activity.isFinishing())
                activity.finish();
        }
        activityList.clear();
    }
}
